<template>
  <div class="app-container">
    <el-card>
      <el-form :model="form" ref="form" :rules="rules" label-width="120px" size="small">
        <el-form-item label="分类" prop="codeId">
          <el-select style="width: 500px" v-model="form.codeId" placeholder="请选择分类" clearable size="small">
            <el-option v-for="item in cateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input style="width: 500px" v-model="form.title" maxlength="50" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Tinymce :readonly="isDetail" ref="tinymce" v-model="form.content" :height="600" />
        </el-form-item>

        <el-form-item label="发布平台：" prop="platformType">
          <el-checkbox-group v-model="form.platformType">
            <el-checkbox v-for="city in platformTypeList" :label="city.dictValue" :key="city.dictValue">{{
              city.dictLabel }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item>
          <el-button v-debounce :disabled="isDetail" type="primary" @click="onSubmit('form')">提交</el-button>
          <el-button @click="goBack()">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addToolLoans, getToolLoans, editToolLoans } from "@/api/tool";
import Tinymce from "@/components/Tinymce";
export default {
  components: { Tinymce },
  data() {
    return {
      form: {
        codeId: "",
        title: "",
        content: "",
        platformType: ['2'],
        toolType: "hui",
      },
      rules: {
        codeId: [{ required: true, message: "请选择银行", trigger: "blur" }],
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        platformType: [
          { required: true, message: "请选择发布平台", trigger: "blur" },
        ],
        content: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
      },
      cateList: [],
      platformTypeList: [],
      isDetail: false
    };
  },
  async created() {
    this.platformTypeList = await this.$getDictData("platform_type");
    this.cateList = await this.$getDictData("hui");
    if (this.$route.query.id) {
      this.$route.meta.title = "编辑";
      this.getInfo();

    }
    if(this.$route.query.id && this.$route.query.type == 'detail') {
      this.$route.meta.title = "查看";
      this.getInfo();
      this.isDetail = true
    }

  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    getInfo() {
      getToolLoans(this.$route.query.id).then((res) => {
        res.data.platformType = res.data.platformType.split(",");
        this.$refs.tinymce.setContent(res.data.content);
        if(this.isDetail) {
          this.$refs.tinymce.setRead(true)
        }
        this.form = Object.assign({}, res.data);
      });
    },
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.form);
          data.platformType = data.platformType.join(",");
          if (this.form.id) {
            editToolLoans(data).then((res) => {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.$router.go(-1);
            });
          } else {
            addToolLoans(data).then((res) => {
              this.$message({
                type: "success",
                message: "添加成功!",
              });
              this.$router.go(-1);
            });
          }
        }
      });
    },
    getList() { },
  },
};
</script>

<style></style>
